package com.mystudy.leetcode.problem.tree.p_590;

import com.mystudy.leetcode.base.Node;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: infoalgorithm
 * @description: N叉树的后序遍历
 * @author: zhouzhilong
 * @create: 2019-07-23 16:07
 **/
public class Solution {






    /**
     * 递归算法，最快的算法
     * @param root
     * @return
     */
    public List<Integer> postorder(Node root) {
        List<Integer> list = new ArrayList<>();
        postorder(root,list);
        return list;
    }

    private void postorder(Node node,List<Integer> list){
        if(node == null){
            return;
        }
        //后序遍历是遍历子树，再遍历根节点
        List<Node> childrens = node.children;
        for(Node n:childrens){
            postorder(n,list);
        }
        list.add(node.val);
    }
}
